#include <QIcon>
#include <QLabel>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QPushButton>
#include <QFileDialog>
#include "mimainwindow.h"
#include "bedit.h"
MIMainWindow::MIMainWindow()
{
    tabbar = new QTabWidget( this );
    tabbar->setTabsClosable( true );

    connect( tabbar, SIGNAL(tabCloseRequested(int)), SLOT(slotTabClosing(int)) );
    connect( tabbar, SIGNAL(currentChanged(int)), SLOT(slotTabChanged(int)) );

    setCentralWidget( tabbar );

}

void MIMainWindow::slotTabChanged( int tab )
{

    setWindowTitle( tr("MuddyIDE - %1").arg( tabbar->tabText(tab) ) );
}

void MIMainWindow::slotTabClosing( int tab )
{
    tabbar->removeTab( tab );

    if( tabbar->count() == 0 ) gettingStartedTab();
}

void MIMainWindow::slotCloseCurrentTab( bool nu )
{

    tabbar->removeTab( tabbar->currentIndex() );

    if( tabbar->count() == 0 ) gettingStartedTab();

}

void MIMainWindow::slotNewProject()
{

}

void MIMainWindow::slotNewTab()
{
    BadgerEditor* widget = new BadgerEditor;
    QIcon icon = QIcon(":/images/document-icon.png");
    int newt = tabbar->addTab( widget, icon, tr("untitled") );
    tabbar->setCurrentIndex( newt );
}

void MIMainWindow::slotOpenFile()
{

	QString ofile = QFileDialog::getOpenFileName( 0, tr("Open file"), QString(), tr("FreeBASIC Files (*.bas *.bi);;All files (*.*)"), 0 );

	if(!ofile.isEmpty()){

		BadgerEditor* widget = new BadgerEditor;
		widget->loadFile( ofile );
		QIcon icon = QIcon(":/images/document-icon.png");
		int newt = tabbar->addTab( widget, icon, ofile );
		tabbar->setCurrentIndex( newt );
	}
}

void MIMainWindow::gettingStartedTab( )
{

    QGroupBox* gb = new QGroupBox();
    QGroupBox* rp = new QGroupBox(tr("Recent Projects"));
    QGroupBox* rf = new QGroupBox(tr("Recent Files"));

    //recent files
    QVBoxLayout* vb1 = new QVBoxLayout;

    QPushButton* butNewFile = new QPushButton( tr("New File...") );
    connect( butNewFile, SIGNAL(clicked()), this, SLOT(slotNewTab()) );

    QLabel* widget = new QLabel("Temporary");

    vb1->addWidget(butNewFile);
    vb1->addWidget(widget);

    rf->setLayout( vb1 );

    //recent projects
    QVBoxLayout* vb2 = new QVBoxLayout;

    QPushButton* butNewProject = new QPushButton( tr("New Project...") );
    connect( butNewProject, SIGNAL(clicked()), this, SLOT(slotNewProject()) );

    QLabel* widget1 = new QLabel("Temporary");

    vb2->addWidget(butNewProject);
    vb2->addWidget(widget1);

    rp->setLayout( vb2 );

    //main
    QGridLayout* gl = new QGridLayout;
    QHBoxLayout* hb = new QHBoxLayout;

    hb->addWidget( rf );
    hb->addWidget( rp );

    QLabel* welcome = new QLabel;
    welcome->setText(tr(
                        "<center><h1>Welcome to MuddyIDE</h1><br />"
                        "blah blah blah blah<br />"
                        "blah blah blah blah<br />"
                        "<img src=\":/images/muddy_ide2.png\" width=100 height=100 /></center>" ) );

    gl->addWidget( welcome,1, 1 );
    gl->addLayout( hb, 2, 1 );

    gb->setLayout( gl );

    QIcon icon = QIcon(":/images/gs.png");
    int newt = tabbar->addTab( gb, icon, tr("Getting Started") );
    tabbar->setCurrentIndex( newt );
}
